package datapack

import (
	"fmt"
	"miniDW/src/log"
)

type Condition struct {
	Field    string
	Operator string
	Value    string
}

func NewCondition(param interface{}) (*Condition, error) {
	cond := &Condition{}
	if c, ok := param.(map[string]interface{}); ok {
		for k, v := range c {
			switch k {
			case "field":
				cond.Field = v.(string)
			case "operator":
				cond.Operator = v.(string)
			case "value":
				cond.Value = v.(string)
			default:
				return nil, log.Error(fmt.Errorf("unknown condition key: %v", k))
			}
		}
	}
	return cond, nil
}

func (cond *Condition) Copy() *Condition {
	newCond := *cond
	return &newCond
}

func (cond *Condition) String() string {
	return cond.Field + cond.Operator + cond.Value
}
